﻿using System;
using System.Collections.Generic;
using System.Security.Claims;
using DataBaseConnect.App;
using Entity;
using Entity.Dto;
using Entity.ViewModels;
using Microsoft.AspNetCore.Mvc;
using Utils;

namespace CoreMVC.Jurisdiction.Manage.Controllers
{
    public class FlowController : BaseController
    {

        #region 构造实例化

        private readonly IFlowAppService _flowAppService;
        private IUserAppService _userAppService;
        private IFormAppService _formAppService;

        public FlowController(IFlowAppService flowAppService
            , IUserAppService userAppService, IFormAppService formAppService)
        {
            _flowAppService = flowAppService;
            _userAppService = userAppService;
            _formAppService = formAppService;
        }

        #endregion

        #region 视图区

        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 查看页
        /// </summary>
        /// <returns></returns>
        public IActionResult SeeFlow()
        {
            return View();
        }

        /// <summary>
        /// 添加页
        /// </summary>
        /// <returns></returns>
        public IActionResult AddFlow()
        {
            //var list = _userAppService.GetUserOrDepartment("");
            List<UserDto> userDto = _userAppService.GetUserOrDepartment("");
            List<FlowType> flowTypes = _flowAppService.GetFlowTypesAll();
            List<Form> forms = _formAppService.GetFormList("");
            var customerViewModel = new UserOrFlowTypeModel(userDto, flowTypes, forms);
            return View(customerViewModel);
        }

        #endregion

        #region 功能实现区

        /// <summary>
        /// 获取所有流程和根据流程名称查询
        /// </summary>
        /// <param name="Name">表单名称</param>
        /// <returns></returns>
        public JsonResult GetFlowList(string Name)
        {
            ReceiveStatus<Flow> receiveStatus = new ReceiveStatus<Flow>();
            List<Flow> list = new List<Flow>();
            try
            {
                //得到所有流程
                list = _flowAppService.GetFlowList(Name);
                receiveStatus.msg = "成功";
                receiveStatus.data = list;
            }
            catch (Exception ex)
            {
                receiveStatus.msg = "失败：" + ex.Message;
                receiveStatus.code = statuCode.Error;
            }
            return Json(receiveStatus);
        }

        /// <summary>
        /// 根据主键获取流程
        /// </summary>
        /// <param name="Id">id</param>
        /// <returns></returns>
        public JsonResult GetFlowId(int Id)
        {
            ReceiveStatus<Flow> receiveStatus = new ReceiveStatus<Flow>();
            try
            {
                var flow = _flowAppService.GetFlowId(Id);
                receiveStatus.msg = "成功";
                receiveStatus.data = flow;
            }
            catch (Exception ex)
            {
                receiveStatus.msg = "失败：" + ex.Message;
                receiveStatus.code = statuCode.Error;
            }
            return Json(receiveStatus);
        }

        /// <summary>
        /// 添加流程信息
        /// </summary>
        /// <param name="Input">参数实体</param>
        /// <returns></returns>
        public JsonResult AddFlowMsg(Flow Input)
        {
            Flow flow = new Flow();
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                Input.CreateTime = DateTime.Now;
                //Input.Id = 1;
                Input.CreateUser = Convert.ToInt32(User.FindFirst(ClaimTypes.Sid).Value);
                var result = _flowAppService.AddFlowMsg(Input);
                receiveStatus.msg = result.msg;
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = "新增失败：" + ex.Message;
            }
            return Json(receiveStatus);
        }
        #endregion
    }
}